/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.generatepage.web.GenerateTab3Action
   Module Description   :

   Date Created      : 2010/2/5
   Original Author   : jeff.ma
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.generatepage.web;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.core.menu.service.MenuService;
import com.yaodian100.ec.category.entity.Category;
import com.yaodian100.ec.category.service.CategoryService;
import com.yaodian100.ec.common.web.AbstractAction;

/**
 * @author jeff.ma
 * 
 */
public class GenerateTab3Action extends AbstractAction {

	/** serialVersionUID */
	private static final long serialVersionUID = -7873900410454076117L;
	@Autowired
	private MenuService menuService;
	@Autowired
	private CategoryService categoryService;
	private String prefix = "Generate.tab3.";

	/* 由FORM传入之查询条件 */
	private String[] paramLevel = new String[] { "0", "1" };
	private String paramName;

	private List<Category> categoryList;
	private Menu menu;
	private String categoryId;

	/** default constructor */
	public GenerateTab3Action() {
	}

	/**
	 * @param prefix the prefix to set
	 */
	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}

	/**
	 * @return the paramLevel
	 */
	public String[] getParamLevel() {
		return paramLevel;
	}

	/**
	 * @param paramLevel the paramLevel to set
	 */
	public void setParamLevel(String[] paramLevel) {
		this.paramLevel = paramLevel;
	}

	/**
	 * @return the paramName
	 */
	public String getParamName() {
		return paramName;
	}

	/**
	 * @param paramName the paramName to set
	 */
	public void setParamName(String paramName) {
		this.paramName = paramName;
	}

	/**
	 * @return the categoryId
	 */
	public String getCategoryId() {
		return categoryId;
	}

	/**
	 * @param categoryId the categoryId to set
	 */
	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}

	/**
	 * @return the categoryList
	 */
	public List<Category> getCategoryList() {
		return categoryList;
	}

	/**
	 * @return the menu
	 */
	public Menu getMenu() {
		return menu;
	}

	/**
	 * 储存物件资料
	 * 
	 * @return
	 * @throws Exception
	 */
	public String save() throws Exception {
		try {
			if (menu != null) {
				menuService.save(menu);
			} else {
				addActionError("物件不存在");
				return search();
			}
		} catch (Exception e) {
			addActionError("储存失败：" + e.getMessage());
			e.printStackTrace();
			return "edit";
		}
		return "view";
	}

	public String list() throws Exception {
		return "list";
	}

	/**
	 * 載入新增物件資料頁
	 * 
	 * @return
	 */
	public String add() {
		menu = new Menu();
		menu.addOption("tab1", "");
		menu.addOption("tab2", "");
		menu.addOption("tab3", "");
		return "edit";
	}

	/**
	 * 修改物件資料
	 * 
	 * @return
	 * @throws Exception
	 */
	public String edit() throws Exception {
		if (menu == null) {
			addActionError("物件不存在, 重新选取");
			return search();
		}
		return "edit";
	}

	/**
	 * 檢視物件資料
	 * 
	 * @return
	 */
	public String view() throws Exception {
		if (StringUtils.isNotBlank(categoryId)) {
			if ("0".equals(categoryId)) {
				menu = menuService.get("Generate.tab3.index");
			} else {
				menu = menuService.get(prefix + categoryId);
			}
		}
		if (menu == null) {
			Category category = categoryService.get(new Long(categoryId)) ;
			menu = new Menu(prefix + categoryId, category.getName() + " Tab3 维护");
			menu.addOption("tab1", "");
			menu.addOption("tab2", "");
			menu.addOption("tab3", "");
		}
		return "view";
	}

	public String search() throws Exception {
		try {
			CommonCriteria cri = new CommonCriteria();
			if (StringUtils.isNotBlank(paramName)) {
				cri.addRlike("name", paramName);
			}
			if (paramLevel != null && paramLevel.length == 1) {
				cri.addEq("level", paramLevel[0]);
			} else {
				cri.addIn("level", Arrays.asList("0", "1"));
			}
			cri.addEq("isVisible", "Y");

			int entitySize = categoryService.getListSize(cri).intValue();
			getPager().setTotalSize(entitySize);
			categoryList = categoryService.getList(getPager().getCurrentPage() * getPager().getPageRecord(), getPager()
					.getPageRecord(), cri, new String[] { "level asc", "showSeq asc" });
		} catch (Exception e) {
			addActionError("搜寻失败：" + e.getMessage());
			e.printStackTrace();
		}
		return "list";
	}

	public String delete() throws Exception {
		try {
			if (menu == null) {
				addActionError("物件不存在");
				return search();
			}
			if ("Generate.tab3.index".equals(menu.getKey())) {
				addActionError("首页Tab3不可删除");
				return "view";
			}
			menuService.delete(menu);
		} catch (Exception e) {
			addActionError("删除失败：" + e.getMessage());
			e.printStackTrace();
			return "view";
		}
		return search();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yaodian100.ec.common.web.AbstractAction#prepare()
	 */
	@Override
	public void prepare() throws Exception {
		super.prepare();
		clearErrorsAndMessages();
	}
}
